<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="店铺名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入店铺名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="详细地址" prop="address">
        <el-input v-model="queryParams.address" placeholder="请输入店铺详细地址" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="审核状态" prop="auditStatus">
        <el-select v-model="queryParams.auditStatus" placeholder="请选择审核状态" clearable>
          <el-option v-for="dict in dict.type.audit_status" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['merchant:shop:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="shopList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="店铺名称" align="center" prop="name" />
      <el-table-column label="经营区域" align="center" prop="operatingArea" />
      <el-table-column label="所属区域" align="center" prop="areaInfo.areaName">
        <template slot-scope="scope">
          <span>{{ scope.row.areaInfo ? scope.row.areaInfo.areaName : '' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="注册时间" align="center" prop="regTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.regTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="开始营业时间" align="center" prop="startTime" width="180">
      </el-table-column>
      <el-table-column label="停止营业时间" align="center" prop="endTime" width="180">
      </el-table-column>
      <el-table-column label="店铺详细地址" align="center" prop="address" />
      <el-table-column label="店铺描述信息" align="center" prop="description" />
      <el-table-column label="店铺照片" align="center" prop="photo" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.photo" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="评分" align="center" prop="rate">
        <template slot-scope="scope">
          <span>{{ scope.row.rate===0?'暂无评分':scope.row.rate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="经营状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.shop_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="auditStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.audit_status" :value="scope.row.auditStatus" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- 审核状态为1（系统已审核）或3（审核不通过）时显示查看按钮 -->
          <el-button v-if="scope.row.auditStatus === 1 || scope.row.auditStatus === 3" size="mini" type="text"
            icon="el-icon-view" @click="handleView(scope.row)" v-hasPermi="['merchant:shop:view']">查看</el-button>
          <!-- 审核状态为2（人工审核）时显示审核按钮 -->
          <el-button v-else-if="scope.row.auditStatus === 2" size="mini" type="text" icon="el-icon-check"
            @click="handleAudit(scope.row)" v-hasPermi="['merchant:shop:audit']">审核</el-button>
          <!-- 其他状态显示修改按钮 -->
          <el-button v-else size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['merchant:shop:edit']">修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />


    <!-- 查看店铺信息对话框 -->
    <el-dialog title="查看店铺信息" :visible.sync="viewOpen" width="800px" append-to-body>
      <el-tabs v-model="activeTab">
        <el-tab-pane label="店铺信息" name="shop">
          <el-form :model="viewForm" label-width="100px" :disabled="true">
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="店铺名称">
                  <el-input v-model="safeShopInfo.name" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="经营区域">
                  <el-input v-model="safeShopInfo.operatingArea" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="详细地址">
                  <el-input v-model="safeShopInfo.address" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="所属区域">
                  <el-input v-model="safeShopInfo.area" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="门店状态">
                  <dict-tag :options="dict.type.shop_status" :value="safeStatus" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="审核状态">
                  <dict-tag :options="dict.type.audit_status" :value="safeAuditStatus" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-form-item label="店铺描述">
              <el-input v-model="safeShopInfo.description" type="textarea" :rows="3" />
            </el-form-item>
            <el-form-item label="店铺照片">
              <image-preview :src="safeShopInfo.photo" :width="200" :height="150" />
            </el-form-item>
          </el-form>
        </el-tab-pane>

        <el-tab-pane label="认证信息" name="verification">
          <el-form :model="viewForm" label-width="100px" :disabled="true">
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="营业执照号">
                  <el-input v-model="safeVerificationInfo.businessLicenseId" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="法人姓名">
                  <el-input v-model="safeVerificationInfo.director" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="联系电话">
                  <el-input v-model="safeVerificationInfo.mobile" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="身份证号">
                  <el-input v-model="safeVerificationInfo.idCard" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-form-item label="营业执照照片">
              <image-preview :src="safeVerificationInfo.businessLicenseImg" :width="200" :height="150" />
            </el-form-item>
            <el-form-item label="身份证照片(头像)">
              <image-preview :src="safeVerificationInfo.front" :width="200" :height="150" />
            </el-form-item>
            <el-form-item label="身份证照片(国徽)">
              <image-preview :src="safeVerificationInfo.opposite" :width="200" :height="150" />
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>
      <div slot="footer" class="dialog-footer">
        <el-button @click="viewOpen = false">关 闭</el-button>
      </div>
    </el-dialog>

    <!-- 审核店铺信息对话框 -->
    <el-dialog title="审核店铺信息" :visible.sync="auditOpen" width="800px" append-to-body>
      <el-tabs v-model="auditActiveTab">
        <el-tab-pane label="店铺信息" name="shop">
          <el-form :model="viewForm" label-width="100px" :disabled="true">
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="店铺名称">
                  <el-input v-model="safeShopInfo.name" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="经营区域">
                  <el-input v-model="safeShopInfo.operatingArea" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="详细地址">
                  <el-input v-model="safeShopInfo.address" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="所属区域">
                  <el-input v-model="safeAreaName" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="门店状态">
                  <dict-tag :options="dict.type.shop_status" :value="safeStatus" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="审核状态">
                  <dict-tag :options="dict.type.audit_status" :value="safeAuditStatus" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-form-item label="店铺描述">
              <el-input v-model="safeShopInfo.description" type="textarea" :rows="3" />
            </el-form-item>
            <el-form-item label="店铺照片">
              <image-preview :src="safeShopInfo.photo" :width="200" :height="150" />
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="认证信息" name="verification">
          <el-form :model="viewForm" label-width="100px" :disabled="true">
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="营业执照号">
                  <el-input v-model="safeVerificationInfo.businessLicenseId" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="法人姓名">
                  <el-input v-model="safeVerificationInfo.director" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="联系电话">
                  <el-input v-model="safeVerificationInfo.mobile" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="身份证号">
                  <el-input v-model="safeVerificationInfo.idCard" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-form-item label="营业执照照片">
              <image-preview :src="safeVerificationInfo.businessLicenseImg" :width="200" :height="150" />
            </el-form-item>
            <el-form-item label="身份证照片(头像)">
              <image-preview :src="safeVerificationInfo.front" :width="200" :height="150" />
            </el-form-item>
            <el-form-item label="身份证照片(国徽)">
              <image-preview :src="safeVerificationInfo.opposite" :width="200" :height="150" />
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>
      <div slot="footer" class="dialog-footer">
        <el-button type="success" @click="handleAuditPass">审核通过</el-button>
        <el-button type="danger" @click="handleAuditReject">审核不通过</el-button>
        <el-button @click="auditOpen = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    // listShop,
    listAllShop,
    getShop,
    delShop,
    addShop,
    updateShop,
    listShopverificationAll,
    auditStatusTrue,
    auditStatusFalse
  } from "@/api/merchant/shop"
  import {
    getShopverification,
  } from "@/api/merchant/shopverification"


  export default {
    name: "Shop",
    dicts: ['shop_status', 'audit_status', 'deleted'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 店铺信息表格数据
        shopList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 是否显示查看弹出层
        viewOpen: false,
        // 是否显示审核弹出层
        auditOpen: false,
        // 查看表单数据
        viewForm: {
          shopInfo: {
            areaInfo: {
              id: '',
              areaName: '',
              del: ''
            },
            id: '',
            name: '',
            operatingArea: '',
            regTime: '',
            startTime: '',
            endTime: '',
            address: '',
            description: '',
            photo: '',
            rate: '',
            area: '',
            status: '',
            deleted: '',
            auditStatus: ''
          },
          id: '',
          mid: '',
          director: '',
          idCard: '',
          mobile: '',
          businessLicenseId: '',
          businessLicenseImg: '',
          front: '',
          opposite: ''
        },
        // 当前查看的标签页
        activeTab: 'shop',
        // 当前审核的标签页
        auditActiveTab: 'shop',
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          name: null,
          address: null,
          auditStatus: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      }
    },
    computed: {
      // 安全访问店铺信息
      safeShopInfo() {
        return this.viewForm && this.viewForm.shopInfo ? this.viewForm.shopInfo : {}
      },
      // 安全访问区域名称
      safeAreaName() {
        return this.safeShopInfo.areaInfo && this.safeShopInfo.areaInfo.areaName ? this.safeShopInfo.areaInfo.areaName : ''
      },
      // 安全访问状态
      safeStatus() {
        return this.safeShopInfo ? this.safeShopInfo.status : null
      },
      // 安全访问审核状态
      safeAuditStatus() {
        return this.safeShopInfo ? this.safeShopInfo.auditStatus : null
      },
      // 安全访问认证信息
      safeVerificationInfo() {
        return this.viewForm ? {
          businessLicenseId: this.viewForm.businessLicenseId || '',
          director: this.viewForm.director || '',
          mobile: this.viewForm.mobile || '',
          idCard: this.viewForm.idCard || '',
          businessLicenseImg: this.viewForm.businessLicenseImg || '',
          front: this.viewForm.front || '',
          opposite: this.viewForm.opposite || ''
        } : {
          businessLicenseId: '',
          director: '',
          mobile: '',
          idCard: '',
          businessLicenseImg: '',
          front: '',
          opposite: ''
        }
      }
    },
    created() {
      this.getList()
    },
    methods: {
      /** 查询店铺信息列表 */
      getList() {
        this.loading = true
        listAllShop(this.queryParams).then(response => {
          this.shopList = response.rows
          this.total = response.total
          this.loading = false
        })
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          name: null,
          operatingArea: null,
          regTime: null,
          startTime: null,
          endTime: null,
          address: null,
          longitude: null,
          latitude: null,
          description: null,
          photo: null,
          rate: null,
          area: null,
          status: null,
          deleted: null,
          auditStatus: null
        }
        this.resetForm("form")
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm")
        this.handleQuery()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },

      /** 修改按钮操作 */
      // handleUpdate(row) {
      //   this.reset()
      //   const id = row.id || this.ids
      //   getShop(id).then(response => {
      //     this.form = response.data
      //     this.open = true
      //     this.title = "修改店铺信息"
      //   })
      // },
      /** 提交按钮 */
      /*  submitForm() {
          this.$refs["form"].validate(valid => {
            if (valid) {
              if (this.form.id != null) {
                updateShop(this.form).then(response => {
                  this.$modal.msgSuccess("修改成功")
                  this.open = false
                  this.getList()
                })
              } else {
                addShop(this.form).then(response => {
                  this.$modal.msgSuccess("新增成功")
                  this.open = false
                  this.getList()
                })
              }
            }
          })
        }, */

      /** 导出按钮操作 */
      handleExport() {
        this.download('merchant/shop/export', {
          ...this.queryParams
        }, `shop_${new Date().getTime()}.xlsx`)
      },
      /** 查看按钮操作 */
      handleView(row) {
        this.reset()
        const mid = row.id
        listShopverificationAll(mid).then(response => {
          // 根据API返回的数据结构，数据在rows数组中
          this.viewForm = response.rows && response.rows.length > 0 ? response.rows[0] : {}
          this.viewOpen = true
        })
      },
      /** 审核按钮操作 */
      handleAudit(row) {
        this.reset()
        const mid = row.id
        listShopverificationAll(mid).then(response => {
          // 根据API返回的数据结构，数据在rows数组中
          this.viewForm = response.rows && response.rows.length > 0 ? response.rows[0] : {}
          this.auditOpen = true
        })
      },
      /** 获取认证信息 */
      /* getVerificationInfo(shopId) {
        // 根据店铺ID查询认证信息
        getShopverification(shopId).then(response => {
          if (response.data) {
            this.verificationForm = response.data
          } else {
            this.verificationForm = {}
          }
        }).catch(() => {
          this.verificationForm = {}
        })
      }, */
      /** 获取审核认证信息 */
      /* getAuditVerificationInfo(shopId) {
         // 根据店铺ID查询认证信息
         getShopverification(shopId).then(response => {
           if (response.data) {
             this.auditVerificationForm = response.data
           } else {
             this.auditVerificationForm = {}
           }
         }).catch(() => {
           this.auditVerificationForm = {}
         })
       }, */
      /** 审核通过 */
      handleAuditPass() {
        this.$modal.confirm('确认审核通过该店铺？').then(() => {
          const updateData = {
            mid:this.viewForm.mid,
            mobile:this.viewForm.mobile,
          }
          auditStatusTrue(updateData).then(response => {
            this.$modal.msgSuccess("审核通过")
            this.auditOpen = false
            this.getList()
          })
        })
      },
      /** 审核不通过 */
      handleAuditReject() {
        this.$modal.confirm('确认审核不通过该店铺？').then(() => {
          const updateData = {
            mid:this.viewForm.mid,
            mobile:this.viewForm.mobile,
          }
          auditStatusFalse(updateData).then(response => {
            this.$modal.msgSuccess("审核不通过")
            this.auditOpen = false
            this.getList()
          })
        })
      }
    }
  }
</script>
